//
//  SRBookPageCollectionViewCell.m
//  StoryShow
//
//  Created by CyonLeuMBP on 2017/4/17.
//  Copyright © 2017年 qudiandu.cn. All rights reserved.
//

#import "SRBookPageCollectionViewCell.h"
#import "FZMCCacheManager.h"

#import "SRBookPageTrackTapView.h"

NSString *const kSRBookPageCollectionViewCell = @"SRBookPageCollectionViewCell";


@interface SRBookPageCollectionViewCell ()

@property (strong, nonatomic) SRBookPageModel *pageModel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageBottomConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageTopConstraint;
@property (weak, nonatomic) IBOutlet UIView *trackContentView;

@property (assign, nonatomic) CGSize imageSize;
@property (strong, nonatomic) NSMutableArray *trackViews;
@property (strong, nonatomic) SRBookPageTrackTapView *clickTrackView;
@property (assign, nonatomic) NSInteger currentPlayTrackIndex;

@end

@implementation SRBookPageCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.currentPlayTrackIndex = 0;
    self.trackViews = [@[] mutableCopy];
}


- (void)updateCellInfo:(SRBookPageModel *)pageModel bookID:(NSString *)bookID {
    if (!pageModel) {
        return;
    }
    
    self.pageModel = pageModel;
    
    UIImage *pageImage = [FZMCCacheManager pageImageWithName:pageModel.pageName bookID:bookID];
    if (!pageImage) {
        WEAKSELF
        [self.pageImageView fz_setImageWithURL:[NSURL URLWithString:pageModel.pageURL] placeholder:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (image) {
                weakSelf.currentPlayTrackIndex = 0;
//                self.pageImageView.image = pageImage;
                CGSize originImageSize = image.size;
                CGFloat imageWidth = SCREEN_WIDTH;
                CGFloat imageHeight = originImageSize.height * imageWidth / originImageSize.width;
                weakSelf.imageSize = CGSizeMake(imageWidth, imageHeight);
                CGFloat verticalSpace = (SCREEN_HEIGHT - imageHeight) / 2.0;
                weakSelf.imageTopConstraint.constant = verticalSpace;
                weakSelf.imageBottomConstraint.constant = verticalSpace;
                weakSelf.clickTrackView = nil;
                
                [weakSelf createTrackViews];
            }
        }];
        
        return;
    }
    self.currentPlayTrackIndex = 0;
    self.pageImageView.image = pageImage;
    CGSize originImageSize = pageImage.size;
    CGFloat imageWidth = SCREEN_WIDTH;
    CGFloat imageHeight = originImageSize.height * imageWidth / originImageSize.width;
    self.imageSize = CGSizeMake(imageWidth, imageHeight);
    CGFloat verticalSpace = (SCREEN_HEIGHT - imageHeight) / 2.0;
    self.imageTopConstraint.constant = verticalSpace;
    self.imageBottomConstraint.constant = verticalSpace;
    self.clickTrackView = nil;
    
    [self createTrackViews];
}

- (void)setClickAreaShow:(BOOL)clickAreaShow {
    _clickAreaShow = clickAreaShow;
    
    if (clickAreaShow) {
        [self changeTrackViewsBorderStyle:SRPageTrackBorderStyleLightBorder];
    } else {
        [self changeTrackViewsBorderStyle:SRPageTrackBorderStyleNone];
    }
}

- (void)removeAllTrackViews {
    [self.trackViews removeAllObjects];
    [self.trackContentView removeAllSubviews];
}

- (void)createTrackViews {
    [self removeAllTrackViews];
    WEAKSELF
    for (int i = 0; i < [self.pageModel.tracks count]; ++i) {
        SRBookPageTrackItemModel *trackModel = self.pageModel.tracks[i];
        
        CGRect trackRect = [self trackRectFromTrackModel:trackModel];
        SRBookPageTrackTapView *trackView = [[SRBookPageTrackTapView alloc] initWithFrame:trackRect];
        trackView.index = i;
        
        [self.trackContentView addSubview:trackView];

        if (self.clickAreaShow) {
            trackView.borderStyle = SRPageTrackBorderStyleLightBorder;
        }
        trackView.trackModel = trackModel;
        
        trackView.touchTrackViewBlock = ^(SRBookPageTrackTapView *tapView, SRBookPageTrackItemModel *itemModel, NSInteger tapIndex) {
                //播放录音
            SRBookPageTrackTapView *lastTrackView = weakSelf.clickTrackView;
            if (lastTrackView && lastTrackView != tapView) {
                if (weakSelf.clickAreaShow) {
                    lastTrackView.borderStyle = SRPageTrackBorderStyleLightBorder;
                } else {
                    lastTrackView.borderStyle = SRPageTrackBorderStyleNone;
                }
            }
            weakSelf.currentPlayTrackIndex = tapIndex;
            weakSelf.clickTrackView = tapView;
            [weakSelf playAudioWithTrackModel:itemModel trackIndex:tapIndex];
        };
        
        [self.trackViews addObject:trackView];
    }
}

- (CGRect)trackRectFromTrackModel:(SRBookPageTrackItemModel *)trackModel {
    CGFloat originX = [trackModel.track_left floatValue] * self.imageSize.width;
    CGFloat originY = [trackModel.track_top floatValue] * self.imageSize.height;
    CGFloat width = ([trackModel.track_right floatValue] - [trackModel.track_left floatValue]) * self.imageSize.width;
    CGFloat height = ([trackModel.track_bottom floatValue] - [trackModel.track_top floatValue]) * self.imageSize.height;

    return CGRectMake(originX, originY, width, height);
}

- (void)changeTrackViewsBorderStyle:(SRPageTrackBorderStyle)borderStyle {
    for (SRBookPageTrackTapView *trackView in self.trackViews) {
        trackView.borderStyle = borderStyle;
    }
}

- (void)playAudioWithTrackModel:(SRBookPageTrackItemModel *)trackModel trackIndex:(NSInteger)trackIndex {
    if (self.playAudioTrackBlock) {
        self.playAudioTrackBlock(trackModel, trackIndex);
    }
}

#pragma mark - Public Method

- (void)playNextTrackModel{
    ++ self.currentPlayTrackIndex;
    if (self.currentPlayTrackIndex >= [self.trackViews count]) {
            //此页已经播放完，播放下一页
        self.currentPlayTrackIndex = [self.trackViews count] - 1;
        if (self.allTracksDidFinishPlayBlock) {
            self.allTracksDidFinishPlayBlock();
        }
    } else {
            //播放下一trackview
        SRBookPageTrackTapView *trackView = [self.trackViews objectAtIndex:self.currentPlayTrackIndex];
        if (trackView ) {
            [trackView onTapView:nil];
        }
    }
}

- (void)playTrackModelWithIndex:(NSInteger)trackIndex {
    self.currentPlayTrackIndex = trackIndex;
    if (self.currentPlayTrackIndex >= [self.trackViews count]) {
            //此页已经播放完，播放下一页
        self.currentPlayTrackIndex = [self.trackViews count] - 1;
        if (self.allTracksDidFinishPlayBlock) {
            self.allTracksDidFinishPlayBlock();
        }
    } else if (self.currentPlayTrackIndex >= 0) {
            //播放下一trackview
        SRBookPageTrackTapView *trackView = [self.trackViews objectAtIndex:self.currentPlayTrackIndex];
        if (trackView ) {
            [trackView onTapView:nil];
        }
    }
}



@end
